package vpn

import (
	"github.com/gorilla/websocket"
	"github.com/kbinani/screenshot"
)

// 常用的公共 STUN 服务器
var stunServers = []string{
	"stun:stun.l.google.com:19302",  // Google
	"stun:stun1.l.google.com:19302", // Google
	"stun:stun2.l.google.com:19302", // Google
	"stun:stun3.l.google.com:19302", // Google
	"stun:stun4.l.google.com:19302", // Google
	"stun:stun.qq.com:3478",         // 腾讯
	"stun:stun.miwifi.com:3478",     // 小米
	"stun:stun.syncthing.net:3478",  // Syncthing
}

type ScreenOrigin struct {
	X int `json:"x" form:"x"`
	Y int `json:"y" form:"y"`
}

// 屏幕参数
type Screen struct {
	Id           string       `json:"id" form:"id"`
	Index        int          `json:"index" form:"index"`
	Label        string       `json:"label" form:"label"`
	Name         string       `json:"name" form:"name"`
	Width        int          `json:"width" form:"width"`
	Height       int          `json:"height" form:"height"`
	RealWidth    int          `json:"realWidth" form:"realWidth"`
	RealHeight   int          `json:"realHeight" form:"realHeight"`
	Scale        float32      `json:"scale" form:"scale"`
	Thumbnail    string       `json:"thumbnail" form:"thumbnail"`
	NativeOrigin ScreenOrigin `json:"nativeOrigin" form:"nativeOrigin"`
}

// 控制指令结构
type ControlCommand struct {
	Type      string `json:"type"` // "mouse", "keyboard", "clipboard"
	X         int    `json:"x,omitempty"`
	Y         int    `json:"y,omitempty"`
	Key       string `json:"key,omitempty"`
	Text      string `json:"text,omitempty"`
	Timestamp int64  `json:"timestamp"`
}

type Client struct {
	Conn        *websocket.Conn `json:"-"`
	Room        string          `json:"room" form:"room"`                 //房间号
	UserId      string          `json:"user_id" form:"user_id"`           //客户端ID
	ControlBy   string          `json:"control_by" form:"control_by"`     //谁在控制
	ControlFlag bool            `json:"control_flag" form:"control_flag"` //是否可以被控制
	PreviewImg  string          `json:"preview_img" form:"preview_img"`   //预览图
	Screens     []Screen        `json:"screens" form:"screens"`           //屏幕参数
}

type Message struct {
	Type string `json:"type"` // "offer", "answer", "ice", "start", "stop", "ready"
	Data any    `json:"data"`
	To   string `json:"to,omitempty"`
	From string `json:"from,omitempty"`
}

// 屏幕参数获取
func GetScreens() []Screen {
	num := screenshot.NumActiveDisplays()
	screens := make([]Screen, 0)
	for i := 0; i < num; i++ {
		bounds := screenshot.GetDisplayBounds(i)
		screens = append(screens, Screen{
			Index:  i,
			Width:  bounds.Dx(),
			Height: bounds.Dy(),
		})
	}
	return screens
}
